package com.avecsys.quotemanager.mbeans.core;

import java.io.Serializable;
import java.util.Date;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.avecsys.quotemanager.constants.ApplicationConstants;
import com.avecsys.quotemanager.dao.common.EnumDBRecordStatus;
import com.avecsys.quotemanager.exception.UserSessionNotFoundException;
import com.avecsys.quotemanager.mbeans.security.LoginMBean;
import com.avecsys.quotemanager.pojos.BasePojo;
import com.avecsys.quotemanager.pojos.UserMasterPojo;
import com.avecsys.quotemanager.ui.components.MessageDialog;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.avecsys.quotemanager.utils.SystemConfig;
import com.icesoft.faces.context.effects.JavascriptContext;

/**
 * @author sam sundar K
 */
public class BaseMBean implements Serializable
{

	private MessageDialog messageDialog = new MessageDialog("messageDialog");

	private boolean screenModeEdit;

	public BaseMBean()
	{
		super();
	}

	/**
	 * Logout Action
	 * 
	 * @param event
	 */
	public static void actionLogout(ActionEvent event)
	{
		FacesContext facesContext = FacesContext.getCurrentInstance();
		facesContext.getExternalContext().getSessionMap().remove(ApplicationConstants.SESSION_USER);
		LoginMBean loginMBean = facesContext.getApplication().evaluateExpressionGet(
				FacesContext.getCurrentInstance(),
					"#{loginMBean}",
					LoginMBean.class);
		loginMBean.setUserMasterPojo(new UserMasterPojo());
		JavascriptContext.addJavascriptCall(
				FacesContext.getCurrentInstance(),
					"parent.window.location.replace('/qm/faces/assets/pages/security/login.xhtml');");
		MessageBoardUtil.resetMessageBoard();
	}

	/**
	 * Method to execute any pre-routines prior to execution of actual method This should be first
	 * statement in method call
	 */
	public void executePreRoutines()
	{
		MessageBoardUtil.resetMessageBoard();
	}

	/**
	 * Method to return the image path. If Image path missing then missing_icon path will be returned
	 */
	public String getImageResourceURL(String resourceName)
	{
		String imageURL = SystemConfig.getResourceURL(resourceName);
		if (imageURL == null)
		{
			imageURL = SystemConfig.getResourceURL(ApplicationConstants.IMAGE_REOURCE_MISSING);
		}
		return imageURL;
	}

	/**
	 * Method to get the decoded menu name value
	 * 
	 * @param menuCode
	 * @return menu Name
	 */
	public String getMenuName(String menuCode)
	{
		String menuName = "#" + menuCode + "#";
		if (SystemConfig.getMenuCode(menuCode) != null)
		{
			menuName = SystemConfig.getMenuCode(menuCode);
		}
		return menuName;
	}

	/**
	 * @return the messageDialog
	 */
	public MessageDialog getMessageDialog()
	{
		return messageDialog;
	}

	/**
	 * @return
	 */
	public String getSecuredOutPutStyleURL()
	{
		UserMasterPojo userMasterPojo = getSignedInUser();
		if (userMasterPojo != null)
		{
			String userType = userMasterPojo.getUserType();
			return SystemConfig.getProperty(ApplicationConstants.APPLICATION_THEME_CSS_SECURED
					+ "."
						+ userType);
		}
		return "";
	}
	
	/**
	 * @return
	 */
	public String getSecuredOutPutBodyClass()
	{
		return SystemConfig.getProperty(ApplicationConstants.APPLICATION_BODY_CLASS);
	}
	
	/**
	 * @return
	 */
	public String getMenuBarEnabled()
	{
		return SystemConfig.getProperty(ApplicationConstants.APPLICATION_MENU_BAR_ENABLED);
	}

	public static UserMasterPojo getSignedInUser()
	{
		UserMasterPojo userMasterPojo = (UserMasterPojo) FacesContext.getCurrentInstance()
				.getExternalContext()
				.getSessionMap()
				.get(ApplicationConstants.SESSION_USER);
		return userMasterPojo;
	}

	/**
	 * @param classKey
	 * @return
	 */
	public String getStyleClass(String classKey)
	{
		return SystemConfig.getProperty(classKey);
	}

	/**
	 * @return
	 */
	public String getUnSecuredOutPutStyleURL()
	{
		return SystemConfig.getProperty(ApplicationConstants.APPLICATION_THEME_CSS_UNSECURED);
	}

	/**
	 * @return the screenModeEdit
	 */
	public boolean isScreenModeEdit()
	{
		return screenModeEdit;
	}

	/**
	 * @param messageDialog the messageDialog to set
	 */
	public void setMessageDialog(MessageDialog messageDialog)
	{
		this.messageDialog = messageDialog;
	}

	/**
	 * @param screenModeEdit the screenModeEdit to set
	 */
	public void setScreenModeEdit(boolean screenModeEdit)
	{
		this.screenModeEdit = screenModeEdit;
	}

	public static void updateUserInformation(BasePojo basePojo, Date currentTimeStamp)
			throws UserSessionNotFoundException
	{
		if (currentTimeStamp == null)
		{
			currentTimeStamp = new Date();
		}
		// Get Logged in user Session instance from session
		UserMasterPojo userMasterPojo = getSignedInUser();
		// If Logged user exists in session
		if (userMasterPojo != null && userMasterPojo.isPersisted())
		{
			if (!basePojo.isPersisted())
			{
				basePojo.setCreatedUserId(userMasterPojo.getUserId());
				basePojo.setCreatedDttm(currentTimeStamp);
			}
			basePojo.setStatus(EnumDBRecordStatus.ACTIVE.status());
			basePojo.setModifiedUserId(userMasterPojo.getUserId());
			basePojo.setModifiedDttm(currentTimeStamp);
		}
		else
		{
			actionLogout(null);
			throw new UserSessionNotFoundException();
		}
	}
}
